#include "iostream"
using namespace std;
struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL){}
};
ListNode* reverseList(ListNode *head){
    ListNode *p=head,*q=head;
    int n=0;
    while (p!=NULL){
        p=p->next;
        n++;
    }
    if (n==0)
        return head;
    int a[n],t=0;
    p=head;
    while (p!=NULL){
        a[t]=p->val;
        t++;
        p=p->next;
    }
    int temp;
    for (int i = 0,j=n-1; i <=j ; ++i,--j) {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
    head->val=a[0];
    for (int k = 1; k < n; ++k) {
        q=q->next;
        q->val=a[k];
    }
    return head;
}

